﻿using IService.Front;

namespace Service.Front
{
    public class RecycleSendService : BaseService<TRecycleSend>, IRecycleSendService
    {
        public RecycleSendService(IUnitOfWorkService unitOfWorkService) : base(unitOfWorkService)
        {
        }

        /// <summary>
        /// 添加或修改
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        public async Task<RM_ApiResult> AddOrUpdate(TRecycleSend par)
        {
            if (par.Id == 0)
            {
                par.CreateTime = DateTime.Now;
            }
            else
            {
                var old = await Get(par.Id);
                if (old != null)
                {
                    par.CreateTime = old.CreateTime;
                }
            }

            var result = await AddOrUpdate(par, true);

            if (result > 0)
            {
                await Status<TRecycleReq>(par.ReqId, (int)Enum_RecycleStatus.Sending);
                await Add<TMessage>(new TMessage
                {
                    CreateTime = DateTime.Now,
                    CustomerId = _CurrentCustomer?.Id ?? 0,
                    AboutId = result,
                    IsSend = false,
                    MsgType = Enum_MessageType.RecycleSend,
                    Remark = "會員已提交快遞信息，請及時處理",
                    EnRemark = "The member has submitted the courier information, please deal with it in time."
                });

                await Delete<TRecycleSendHandle>(o => o.ReqId == par.ReqId);

                return _ResultHelper.BuildActionSuccess();
            }
            else
            {
                return _ResultHelper.BuildActionError();
            }
        }
    }
}